home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / sendmail / sendmail-5.65c+IDA-1.4.4.1 / src / RCS / syslog.c,v < prev    next >
Encoding:
Text File  |  1991-06-24  |  7.5 KB  |  367 lines

  1. head    5.20;
  2. branch    5.20.0;
  3. access;
  4. symbols
  5.     UICSO:5.20.0
  6.     VANILLA:5.20;
  7. locks; strict;
  8. comment    @ * @;
  9.  
  10.  
  11. 5.20
  12. date    91.06.24.20.29.03;    author paul;    state Exp;
  13. branches
  14.     5.20.0.1;
  15. next    ;
  16.  
  17. 5.20.0.1
  18. date    91.06.24.20.30.05;    author paul;    state Exp;
  19. branches;
  20. next    ;
  21.  
  22.  
  23. desc
  24. @System log routines.
  25. @
  26.  
  27.  
  28. 5.20
  29. log
  30. @Initial 5.65 distribution.
  31. @
  32. text
  33. @/*
  34.  * Copyright (c) 1983, 1988 Regents of the University of California.
  35.  * All rights reserved.
  36.  *
  37.  * Redistribution and use in source and binary forms are permitted
  38.  * provided that the above copyright notice and this paragraph are
  39.  * duplicated in all such forms and that any documentation,
  40.  * advertising materials, and other materials related to such
  41.  * distribution and use acknowledge that the software was developed
  42.  * by the University of California, Berkeley.  The name of the
  43.  * University may not be used to endorse or promote products derived
  44.  * from this software without specific prior written permission.
  45.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  46.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  47.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  48.  */
  49.  
  50. #if defined(LIBC_SCCS) && !defined(lint)
  51. static char sccsid[] = "@@(#)syslog.c    5.20 (Berkeley) 1/19/89";
  52. #endif /* LIBC_SCCS and not lint */
  53.  
  54. /*
  55.  * SYSLOG -- print message on log file
  56.  *
  57.  * This routine looks a lot like printf, except that it outputs to the
  58.  * log file instead of the standard output.  Also:
  59.  *    adds a timestamp,
  60.  *    prints the module name in front of the message,
  61.  *    has some other formatting types (or will sometime),
  62.  *    adds a newline on the end of the message.
  63.  *
  64.  * The output of this routine is intended to be read by syslogd(8).
  65.  *
  66.  * Author: Eric Allman
  67.  * Modified to use UNIX domain IPC by Ralph Campbell
  68.  */
  69.  
  70. #include <sys/types.h>
  71. #include <sys/socket.h>
  72. #include <sys/file.h>
  73. #include <sys/signal.h>
  74. #include <sys/syslog.h>
  75. #include <netdb.h>
  76. #include <strings.h>
  77. #include <varargs.h>
  78. #include <stdio.h>
  79.  
  80. #define    LOGNAME    "/dev/log"
  81. #define    CONSOLE    "/dev/console"
  82.  
  83. static int    LogFile = -1;        /* fd for log */
  84. static int    connected;        /* have done connect */
  85. static int    LogStat = 0;        /* status bits, set by openlog() */
  86. static char    *LogTag = "syslog";    /* string to tag the entry with */
  87. static int    LogFacility = LOG_USER;    /* default facility code */
  88.  
  89. syslog(pri, fmt, args)
  90.     int pri, args;
  91.     char *fmt;
  92. {
  93.     vsyslog(pri, fmt, &args);
  94. }
  95.  
  96. vsyslog(pri, fmt, ap)
  97.     int pri;
  98.     register char *fmt;
  99.     va_list ap;
  100. {
  101.     extern int errno;
  102.     register int cnt;
  103.     register char *p;
  104.     time_t now, time();
  105.     int pid, saved_errno;
  106.     char tbuf[2048], fmt_cpy[1024], *ctime();
  107.  
  108.     saved_errno = errno;
  109.  
  110.     /* see if we should just throw out this message */
  111.     if ((u_int)LOG_FAC(pri) >= LOG_NFACILITIES ||
  112.         !LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
  113.         return;
  114.     if (LogFile < 0 || !connected)
  115.         openlog(LogTag, LogStat | LOG_NDELAY, 0);
  116.  
  117.     /* set default facility if none specified */
  118.     if ((pri & LOG_FACMASK) == 0)
  119.         pri |= LogFacility;
  120.  
  121.     /* build the message */
  122.     (void)time(&now);
  123.     (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
  124.     for (p = tbuf; *p; ++p);
  125.     if (LogTag) {
  126.         (void)strcpy(p, LogTag);
  127.         for (; *p; ++p);
  128.     }
  129.     if (LogStat & LOG_PID) {
  130.         (void)sprintf(p, "[%d]", getpid());
  131.         for (; *p; ++p);
  132.     }
  133.     if (LogTag) {
  134.         *p++ = ':';
  135.         *p++ = ' ';
  136.     }
  137.  
  138.     /* substitute error message for %m */
  139.     {
  140.         register char ch, *t1, *t2;
  141.         char *strerror();
  142.  
  143.         for (t1 = fmt_cpy; ch = *fmt; ++fmt)
  144.             if (ch == '%' && fmt[1] == 'm') {
  145.                 ++fmt;
  146.                 for (t2 = strerror(saved_errno);
  147.                     *t1 = *t2++; ++t1);
  148.             }
  149.             else
  150.                 *t1++ = ch;
  151.         *t1 = '\0';
  152.     }
  153.  
  154.     (void)vsprintf(p, fmt_cpy, ap);
  155.  
  156.     /* output the message to the local logger */
  157.     if (send(LogFile, tbuf, cnt = strlen(tbuf), 0) >= 0 ||
  158.         !(LogStat&LOG_CONS))
  159.         return;
  160.  
  161.     /* output the message to the console */
  162.     pid = vfork();
  163.     if (pid == -1)
  164.         return;
  165.     if (pid == 0) {
  166.         int fd;
  167.         long sigsetmask();
  168.  
  169.         (void)signal(SIGALRM, SIG_DFL);
  170.         sigsetmask((long)~sigmask(SIGALRM));
  171.         (void)alarm((u_int)5);
  172.         if ((fd = open(CONSOLE, O_WRONLY, 0)) < 0)
  173.             return;
  174.         (void)alarm((u_int)0);
  175.         (void)strcat(tbuf, "\r");
  176.         p = index(tbuf, '>') + 1;
  177.         (void)write(fd, p, cnt + 1 - (p - tbuf));
  178.         (void)close(fd);
  179.         _exit(0);
  180.     }
  181.     if (!(LogStat & LOG_NOWAIT))
  182.         while ((cnt = wait((int *)0)) > 0 && cnt != pid);
  183. }
  184.  
  185. static struct sockaddr SyslogAddr;    /* AF_UNIX address of local logger */
  186. /*
  187.  * OPENLOG -- open system log
  188.  */
  189. openlog(ident, logstat, logfac)
  190.     char *ident;
  191.     int logstat, logfac;
  192. {
  193.     if (ident != NULL)
  194.         LogTag = ident;
  195.     LogStat = logstat;
  196.     if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
  197.         LogFacility = logfac;
  198.     if (LogFile == -1) {
  199.         SyslogAddr.sa_family = AF_UNIX;
  200.         strncpy(SyslogAddr.sa_data, LOGNAME, sizeof SyslogAddr.sa_data);
  201.         if (LogStat & LOG_NDELAY) {
  202.             LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
  203.             fcntl(LogFile, F_SETFD, 1);
  204.         }
  205.     }
  206.     if (LogFile != -1 && !connected &&
  207.         connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1)
  208.         connected = 1;
  209. }
  210.  
  211. /*
  212.  * CLOSELOG -- close the system log
  213.  */
  214. closelog()
  215. {
  216.     (void) close(LogFile);
  217.     LogFile = -1;
  218.     connected = 0;
  219. }
  220.  
  221. static int    LogMask = 0xff;        /* mask of priorities to be logged */
  222. /*
  223.  * SETLOGMASK -- set the log mask level
  224.  */
  225. setlogmask(pmask)
  226.     int pmask;
  227. {
  228.     int omask;
  229.  
  230.     omask = LogMask;
  231.     if (pmask != 0)
  232.         LogMask = pmask;
  233.     return (omask);
  234. }
  235. @
  236.  
  237.  
  238. 5.20.0.1
  239. log
  240. @ANSIfied and System 5 adaptations done by Bruce Lilly.
  241. @
  242. text
  243. @d18 4
  244. a35 1
  245.  * modified for System V by Bruce Lilly
  246. d38 9
  247. a46 19
  248. #include "sendmail.h"
  249. #ifdef NEED_SYSLOG
  250.  
  251. # if defined(LIBC_SCCS) && !defined(lint)
  252. static char sccsid[] = "@@(#)syslog.c    5.20 (Berkeley) 1/19/89";
  253. # endif /* LIBC_SCCS and not lint */
  254.  
  255. # ifdef SYSV
  256. #  include <string.h>
  257. # else /* !SYSV */
  258. #  include <strings.h>
  259. # endif /* SYSV */
  260. # include <netdb.h>
  261.  
  262. # ifdef __STDC__
  263. void    closelog();
  264. # else /* !__STDC__ */
  265. void    closelog(), vsyslog();
  266. # endif /* __STDC__ */
  267. d48 2
  268. a49 2
  269. # define    LOGNAME    "/dev/log"
  270. # define    CONSOLE    "/dev/console"
  271. a56 2
  272. # ifndef __STDC__
  273. void
  274. d64 1
  275. a64 3
  276. /*VARARGS2*/
  277. void
  278. vsyslog(pri, fmt, va_alist)
  279. d67 2
  280. a68 7
  281.     va_dcl
  282. # else /* __STDC__ */
  283.  
  284. void
  285. syslog(int pri, char *fmt, ...)
  286. # endif /* !__STDC__ */
  287.       va_list ap;
  288. d72 1
  289. a72 1
  290.     time_t now;
  291. d74 1
  292. a74 2
  293.     char tbuf[2048], fmt_cpy[1024];
  294.     unsigned int more_time;
  295. a75 5
  296. # ifdef __STDC__
  297.     va_start(ap, fmt);
  298. # else /* !__STDC__ */
  299.     va_start(ap);
  300. # endif /* __STDC__ */
  301. a80 2
  302.     {
  303.         va_end(ap);
  304. a81 1
  305.     }
  306. d109 1
  307. a122 1
  308.     va_end(ap);
  309. d135 1
  310. a137 1
  311. # ifndef SYSV
  312. d139 1
  313. a139 4
  314. # endif /* !SYSV */
  315.         more_time = 5;
  316.         while (more_time = alarm(more_time))
  317.             ;
  318. a153 1
  319. static struct sockaddr_in SyslogPort;    /* AF_INET address of local logger */
  320. a156 1
  321. void
  322. d175 1
  323. a175 1
  324.         connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1) {
  325. a176 34
  326.         return;
  327.     } else
  328.         if (LogFile != -1)
  329.             (void) close(LogFile);
  330.     if (LogFile == -1) {{
  331.         struct    servent    *se;
  332.         struct    hostent    *host = 0;
  333.         char    hbuf[32];
  334.  
  335.         if (gethostname(hbuf, 31) == 0) {
  336.             strtok(hbuf, ".");
  337.             host = gethostbyname(hbuf);
  338.         }
  339.         if (host == NULL)
  340.             if ((host = gethostbyname("localhost")) == NULL)
  341.                 return;
  342.         if((se = getservbyname("syslog", "udp")) == NULL)
  343.             return;
  344.         SyslogPort.sin_port = se->s_port;
  345.         SyslogPort.sin_family = AF_INET;
  346.         if((LogFile = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  347.             return;
  348.         (void) memcpy((caddr_t)&SyslogPort.sin_addr, (caddr_t)host->h_addr,
  349.             host->h_length);
  350.     }
  351.     if (LogFile != -1 && !connected &&
  352.         connect(LogFile, (struct sockaddr *)&SyslogPort, (int)sizeof(SyslogPort)) != -1) {
  353.         connected = 1;
  354.         return;
  355.     } else {
  356.         LogStat |= LOG_CONS;
  357.         if (LogFile != -1)
  358.             (void) close(LogFile);
  359.     }
  360. a181 1
  361. void
  362. a192 1
  363. int
  364. a202 1
  365. #endif /* NEED_SYSLOG */
  366. @
  367.